import 'package:flutter_yim/YIM.dart';
import 'package:pc/Const/IFinal.dart';
import 'package:pc/Model/MessageModel.dart';
import 'package:pc/Page/Home/Conversation/state.dart';
import 'package:pc/Page/Home/Conversation/view.dart';
import 'package:pc/Util/App/AppUtils.dart';

class ConversationController {
  final page = IFinal.Others.homePage;
  final state = ConversationState();
  final view = ConversationView();

  void init() {
    state.focusNode_search.addListener(() {
      state.isSearching = state.focusNode_search.hasFocus;
      page.setState(() {});
    });
  }

  void dispose() {}

  void queryRecentConversations() {
    YIM.conversationManager.queryRecentConversation(callback: (conversations) async {
      if (conversations == null) {
        page.badge_message_count = 0;
        page.setState(() {});
        return;
      }
      var unReadCount = 0;
      List<ConversationModel> newConversations = [];
      final userModel = await AppUtils.readUserModel();
      final selectConversation = getSelectConversation();
      for (final conversation in conversations) {
        try {
          final newConversation = ConversationModel.fromYIMConversation(conversation, conversation.lastMessage!.from == userModel!.yim_user);
          if (selectConversation == null || newConversation.conversationID != selectConversation.conversationID) {
            unReadCount += conversation.unRead;
          } else {
            newConversation.unReadCount = 0;
          }
          newConversations.add(newConversation);
        } catch (e) {}
      }
      if (state.conversations != null) {
        String? selectConversationID;
        for (var conversation in state.conversations!) {
          if (conversation.isSelect) {
            selectConversationID = conversation.conversationID;
            break;
          }
        }
        if (selectConversationID != null) {
          for (var newConversation in newConversations) {
            if (newConversation.conversationID == selectConversationID) newConversation.isSelect = true;
          }
        }
      }
      state.conversations = newConversations;
      page.badge_message_count = unReadCount;
      page.setState(() {});
    });
  }

  ConversationModel? getSelectConversation({List<ConversationModel>? conversations}) {
    for (var conversation in conversations ?? state.conversations ?? []) {
      if (conversation.isSelect) return conversation;
    }
    return null;
  }

  void clearUnReadCount() {
    final conversation = page.controller_conversation!.getSelectConversation();
    if (conversation != null) {
      YIM.conversationManager.clearUnreadCount(
        conversationType: YIMEnum_ConversationType.P2P,
        conversationID: conversation.conversationID,
      );
    }
  }

  void sendMessageReceipt() {
    final conversation = page.controller_conversation!.getSelectConversation();
    if (conversation != null) {
      YIM.messageManager.sendMessageReceipt(
        conversationType: YIMEnum_ConversationType.P2P,
        conversationID: conversation.conversationID,
      );
    }
  }
}
